﻿using AutoMapper;
using EquipmentModule.Contracts;
using EquipmentModule.Entity;
using EquipmentModule.ViewModel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace EquipmentModule.Controllers
{
    [ApiController]
    [Route("Equipment/[controller]")]
    public class SectionController : ControllerBase
    {
        private readonly ILogger _logger;
        private readonly IWebHostEnvironment _env;
        private readonly ISectionService _service;
        private readonly IEquipmentHistoryService _equipmentHistoryService;
        private readonly IMapper _mapper;

        public SectionController(IMapper mapper, ILogger<SectionController> logger, IWebHostEnvironment env, ISectionService service, IEquipmentHistoryService equipmentHistoryService)
        {
            _logger = logger;
            _env = env;
            _service = service;
            _equipmentHistoryService = equipmentHistoryService;
            _mapper = mapper;
        }
        [HttpGet]
        public IActionResult Index()
        {
            string db = Common.GetHeader4DB(this.Request);
            var list = _service.Index(db).ToList();
            foreach (var item in list)
            {
                var exist = list.Find(x => x.Id == item.ParentId);
                if (exist != null)
                {
                    if (exist.Children == null)
                    {
                        exist.Children = new List<Section>();
                    }
                    exist.Children.Add(item);
                }
            }
            list.RemoveAll(x => x.ParentId != Guid.Empty);
            List<SectionViewModel> sections = new List<SectionViewModel>();
            if (list.Count>0)
            {
                 sections= _mapper.Map<List<Section>, List<SectionViewModel>>(list);
            }
            return Ok(new ResponseData(null, sections));
        }

        [HttpGet("{id}")]
        public IActionResult Index(Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            var section = _service.Index(id, db);
            if (section!=null)
            {
                var info = _mapper.Map<Section, SectionViewModel4Item>(section);
                return Ok(new ResponseData(null, info));
            }
            else
            {
                //可能是设备
                var list = _equipmentHistoryService.GetCurrentData(id,db);
                var list1 = _mapper.Map<List<EquipmentHistoryItem>, List<EquipmentHistoryItemViewModel>>(list);
                return Ok(new ResponseData(null, list1));
            }
        }
        [HttpGet("All")]
        public IActionResult All()
        {
            string db = Common.GetHeader4DB(this.Request);
            var list = _service.Index(db).ToList();
            //获取所有的设备
            var es=_service.IndexEquipment(db).ToList();
            list.InsertRange(0, es);
            foreach (var item in list)
            {
                var exist = list.Find(x => x.Id == item.ParentId);
                if (exist != null)
                {
                    if (exist.Children == null)
                    {
                        exist.Children = new List<Section>();
                    }
                    exist.Children.Add(item);
                }
            }
            list.RemoveAll(x => x.ParentId != Guid.Empty);
            List<SectionViewModel> sections = new List<SectionViewModel>();
            if (list.Count > 0)
            {
                sections = _mapper.Map<List<Section>, List<SectionViewModel>>(list);
            }
            else
            {
                sections.Add(new SectionViewModel() { Id = Guid.Empty, Name = "ROOT" });
            }
            return Ok(new ResponseData(null, sections));
        }

        [HttpDelete]
        public IActionResult Del(Guid id)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.Del(id, db);
            return Ok(new ResponseData(null, null));
        }

        [HttpPost]
        public IActionResult Add(SectionViewModel4Add info)
        {
            string db = Common.GetHeader4DB(this.Request);
            _service.Add(info, db);
            return Ok(new ResponseData(null, null));
        }
    }
}
